package longrise.phone.com.bjjt_henan.main;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.longrise.android.FormParameter;
import com.longrise.android.LFView;
import com.longrise.android.util.Util;
import com.longrise.android.widget.LLinearLayoutView;

import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;

/**
 * 出行服务页面
 */
public class TravelServiceView extends LFView implements
        MainActivity.OnActivityReturnBitmap {

    private static final int RETURN = 0x998;

    private Context context = null;

    private View view = null;

    private Dialog progressDialog;

    private int type;

    private String appCaseno;

    public TravelServiceView(Context c) {
        super(c);
        this.context = c;
        if (context instanceof MainActivity) {
            ((MainActivity) context).setOnActivityReturnBitmap(this);
        }
    }

    public TravelServiceView(Context c, int type) {
        this(c);
        this.type = type;
    }

    public void setAppCaseno(String appCaseno) {
        this.appCaseno = appCaseno;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter parameter = new FormParameter();
        parameter.setShowtitle(false);
        return parameter;
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        if (context == null) {
            return;
        }
        view = new View(context);

        initProgressDialog();
        progressDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface arg0) {
                closeView();
            }
        });
        progressDialog.show();
        // 进入地图界面
        if (context instanceof MainActivity) {
            Intent intent = new Intent(context, TrafficMapActivity.class);
            intent.putExtra("type", type);
            intent.putExtra("appcaseno", appCaseno);
            ((MainActivity) context).startActivityForResult(intent, RETURN);
        }
    }

    /**
     * 实例化进度条
     */
    private void initProgressDialog() {
        this.progressDialog = new Dialog(context, R.style.dialog_style);
        progressDialog.setCanceledOnTouchOutside(false);

        LLinearLayoutView dialogView = new LLinearLayoutView(context);
        dialogView.setBackgroundColor(Color.parseColor("#55000000"), -10, -10,
                -10, -10, Util.dip2px(context, 5), 0,
                Color.parseColor("#55000000"));
        dialogView.setGravity(Gravity.CENTER);
        dialogView.setOrientation(LinearLayout.VERTICAL);

        // 进度条
        ProgressBar bar = new ProgressBar(context);
        bar.setIndeterminate(true);
        bar.setIndeterminateDrawable(context.getResources().getDrawable(
                R.drawable.lcontainerview_anim_loading));
        int w = Util.dip2px(context, 30);
        dialogView.addView(bar, w, w);

        TextView textView = new TextView(context);
        textView.setText("正在获取地理位置...");
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(14);
        textView.setTextColor(Color.parseColor("#FFFFFF"));
        textView.setPadding(0, Util.dip2px(context, 10), 0, 0);
        dialogView.addView(textView);

        int widthDialog = Util.dip2px(context, 100);
        LayoutParams dialogLP = new LayoutParams(widthDialog, widthDialog);
        this.progressDialog.setContentView(dialogView, dialogLP);
        dialogLP = null;
    }

    @Override
    public void OnDestroy() {
        progressDialog = null;
        view = null;
        super.OnDestroy();
    }

    // 关闭当前界面
    private void closeView() {
        if (null != progressDialog && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        OnDestroy();
        closeForm();
        if (type == 1) {
            ((MainActivity) context).backToMainView();
        }
    }

    @Override
    public void refresh() {

    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void activityReturnBitmap(int requestCode, int resultCode,
                                     Intent data) {
        if (requestCode == RETURN) {
            closeView();
        }
    }
}
